package org.acghub.mtdb.xmlgen.maker;

import org.acghub.mtdb.common.Bean;
import org.acghub.mtdb.common.Config;
import org.acghub.mtdb.common.Mtdb;
import org.acghub.mtdb.common.Table;
import org.acghub.mtdb.util.CachedFileOutputStream;
import org.acghub.mtdb.xmlgen.Maker;

import java.util.List;

/**
 * @author zhouhe
 */
public class MtdbMaker extends Maker<Mtdb> {

    public MtdbMaker(Mtdb inner, Config config) {
        super(inner, config);
        List<BeanMaker> beanMakers = this.inner().getChildren(Bean.class).stream().map(bean -> new BeanMaker(bean, this.config())).toList();
        List<TableMaker> tableMakers = this.inner().getChildren(Table.class).stream().map(table -> new TableMaker(table, this.config())).toList();
        TablesMaker tablesMaker = new TablesMaker(this.inner(), this.config());

        this.addChildren(beanMakers);
        this.addChildren(tableMakers);
        this.addChild(tablesMaker);

    }

    @Override
    public void make() throws Exception {
        CachedFileOutputStream.addRemoveFile(this.config().getTableDirFile(), this.config().getBeanDirFile());
        this.childrenMake();
        CachedFileOutputStream.removeFiles();
    }
}
